import 'package:bigegg/common/log/xmm_logcat.dart';
import 'package:bigegg/data/repository/xmm_remote_repository.dart';
import 'package:bigegg/entity/user/xmm_wall_user.dart';
import 'package:bigegg/global/user/xmm_account_manager.dart';
import 'package:bigegg/utils/xmm_toast_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:svgaplayer_flutter/player.dart';

import '../global/routes/xmm_routes.dart';

class XmmGiftDialog extends StatefulWidget {
  const XmmGiftDialog(this.user, {super.key});

  final XmmWallUser user;

  @override
  State<StatefulWidget> createState() {
    return XmmGiftDialogState();
  }
}

class XmmGiftDialogState extends State<XmmGiftDialog> {
  final _selectIndex = 0.obs;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 328.w,
      height: 411.w,
      padding: EdgeInsets.only(top: 79.w, left: 22.w, right: 22.w),
      decoration: const BoxDecoration(
        image: DecorationImage(
          fit: BoxFit.fill,
          image: AssetImage("assets/images/xmm_gift_bg.png"),
        ),
      ),
      child: Column(
        children: [
          Image.asset("assets/images/xmm_icon_gift_title.png", height: 45.h),
          SizedBox(height: 57.w),
          Obx(
            () => Row(
              children: [
                giftWidget("assets/images/xmm_icon_gift_1.png", 48, 1),
                const Expanded(child: SizedBox()),
                giftWidget("assets/images/xmm_icon_gift_2.png", 38, 2),
                const Expanded(child: SizedBox()),
                giftWidget("assets/images/xmm_icon_gift_3.png", 28, 3),
              ],
            ),
          ),
          SizedBox(height: 28.w),
          GestureDetector(
            child: Container(
              alignment: Alignment.center,
              decoration: const BoxDecoration(
                image: DecorationImage(
                  image: AssetImage("assets/images/xmm_icon_post.png"),
                ),
              ),
              width: 155.w,
              height: 54.w,
              child: const Text(
                "Send",
                style: TextStyle(
                  fontSize: 16,
                  color: Color(0xFF202020),
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            onTap: () {
              if (_selectIndex.value == 0) {
                XmmToastUtils.show("Please select one to send");
              } else {
                sendGift();
              }
            },
          )
        ],
      ),
    );
  }

  Future<void> sendGift() async {
    try {
      EasyLoading.show();
      var money = 0;
      if (_selectIndex.value == 1) {
        money = 48;
      } else if (_selectIndex.value == 2) {
        money = 38;
      } else if (_selectIndex.value == 3) {
        money = 28;
      }
      var resp = await XmmRemoteRepository().consumeCoin(money);
      if (resp.success) {
        if (_selectIndex.value == 1) {
          widget.user.giftNumber1 = (widget.user.giftNumber1 ?? 0) + 1;
        } else if (_selectIndex.value == 2) {
          widget.user.giftNumber2 = (widget.user.giftNumber2 ?? 0) + 1;
        } else if (_selectIndex.value == 3) {
          widget.user.giftNumber3 = (widget.user.giftNumber3 ?? 0) + 1;
        }
        XmmAccountManager.instance.updateWallUser();
        XmmAccountManager.instance.user.availableCoins -= money;
        XmmAccountManager.instance.updateUserInfo();
        Navigator.pop(context, true);
        showGiftEffect(_selectIndex.value);
      } else if (resp.code == 20010009) {
        Get.toNamed(XmmRoutes.shoppingCoinPage);
        XmmToastUtils.show("coins is not enough");
      } else {
        XmmXLogcat.e("sendGift ${resp.toString()}");
        XmmToastUtils.show(resp.msg);
      }
    } finally {
      EasyLoading.dismiss();
    }
  }

  void showGiftEffect(int index) {
    String image;
    if (index == 1) {
      image = "assets/images/xmm_gift_effect_1.png";
    } else if (index == 2) {
      image = "assets/images/xmm_gift_effect_2.png";
    } else {
      image = "assets/images/xmm_gift_effect_3.png";
    }
    showDialog(
      context: context,
      builder: (context) {
        Future.delayed(
          const Duration(seconds: 2),
          () => Navigator.pop(context),
        );
        return Center(
          child: Stack(
            alignment: Alignment.center,
            children: [
              const SVGASimpleImage(
                assetsName: 'assets/images/svga/xmm_gift_bg.svga',
              ),
              Image.asset(
                image,
                height: 283.w,
                width: 269.w,
              ),
            ],
          ),
        );
      },
    );
  }

  Widget giftWidget(String image, int money, int index) {
    return GestureDetector(
      child: Container(
        width: 80.w,
        height: 134.w,
        padding: EdgeInsets.only(top: 8.w, left: 5.w, right: 8.w, bottom: 10.w),
        decoration: BoxDecoration(
          border: Border.all(
            color: index == _selectIndex.value ? Colors.red : Colors.white,
            width: 1,
          ),
          color: const Color(0xFF15E8C2),
          borderRadius: BorderRadius.circular(14),
        ),
        child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
          Image.asset(image, width: 65.w, height: 70.w),
          const Expanded(child: SizedBox()),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image.asset(
                "assets/images/xmm_icon_money.png",
                width: 17.w,
                height: 17.w,
              ),
              const SizedBox(width: 4),
              Text(
                "$money",
                style: TextStyle(
                  fontSize: 14.sp,
                  color: Color(0xFF202020),
                ),
              ),
            ],
          )
        ]),
      ),
      onTap: () => _selectIndex.value = index,
    );
  }
}
